<?php
class ModelBase{
    private static $model;
    private $table;
    private $where;
    private $limit;
    private $orderBy;
    private $filed;

    private $sql;
    private $config;

    public function __construct($table){
        $this->table=$table;
        $this->config=include(dirname(__DIR__).'/configs/db.php');
        $this->config['table']=$table;

        include "DB/DB.php";
        $this->db=DB::factory($this->config['driver']);
        $this->db->connet($this->config);
    }

    public static function getInstance($table){
        if(self::$model){
            return self::$model;
        }else{
            if(file_exists(dirname(__DIR__).'/models/'.ucfirst($table).'Model')){
                include(dirname(__DIR__).'/models/'.ucfirst($table).'Model.php');
                $model=ucfirst($table).'Model';
                self::$model=new $model($table);
                return self::$model;
            }else{
                return self::$model=new ModelBase($table);
            }
        }
    }


    public function where($array){
        $this->where=$array;
        return $this;
    }

    public function orderBy($stirng){
        $this->orderBy=$stirng;
        return $this;
    }

    public function limit($number){
        $this->limit=$number;
        return $this;
    }

    public function field($filds){
        $this->filed=$filds;
        return $this;
    }


    public function find(){
       $result=$this->select();
        $this->sql=$this->getLastSql()." LIMIT 1";
        if($result){
            return $result[0];
        }else{
            return null;
        }

    }

    public function select(){
        $sql='SELECT';
        if($this->filed){
            foreach($this->filed as $file){
                $sql=$sql.' '.$file.' ';
            }
        }else{
            $sql=$sql.' * ';
        }

        $sql=$sql.' from '.$this->table;


        if($this->where){
            $sql = $sql.' WHERE ';
            foreach($this->where as $key => $value){
                if(is_array($value[0])){
                    $key=$value[0];
                    if(is_string($value[1])){
                        $value[1]='"'.$value[1].'"';
                    }
                    switch ($key){
                        case 'eq':
                            $sql=$sql.' '.$key.'='.$value[1].' AND';
                            break;
                        case 'lt':
                            $sql=$sql.' '.$key.'<'.$value[1].' AND';
                            break;
                        case 'elt':
                            $sql=$sql.' '.$key.'<='.$value[1].' AND';
                            break;
                        case 'gt':
                            $sql=$sql.' '.$key.'>'.$value[1].' AND';
                            break;
                        case 'egt':
                            $sql=$sql.' '.$key.'>='.$value[1].' AND';
                            break;
                    }
                }else{
                    if(is_string($value)){
                        $value='"'.$value.'"';
                    }
                    $sql=$sql.' '.$key.'='.$value.' AND';
                }
            }
            //把最后一个AND删除
            $sql=rtrim($sql,'AND');
        }

        if($this->orderBy){
            $sql=$sql.' ORDERBY '.$this->orderBy.' ';
        }

        if($this->limit){
            $sql=$sql.' LIMIT '.$this->limit.' ';
        }
        $this->sql=$sql;
        $this->unsetWOL();

        return $this->db->query($sql);
    }

    public function save($data){
        $sql='update '.$this->table;

        if($data){
            $sql=$sql.' SET ';

            foreach($data as $key => $value){
                if(is_string($value)){
                    $value='"'.$value.'"';
                }
                $sql=$sql.' '.$key.'='.$value.',';
            }

            //把最后一个,删除
            $sql=rtrim($sql,',');
        }


        if($this->where){
            $sql = $sql.' WHERE ';
            foreach($this->where as $key => $value){
                if(is_array($value[0])){
                    $key=$value[0];
                    if(is_string($value[1])){
                        $value[1]='"'.$value[1].'"';
                    }
                    switch ($key){
                        case 'eq':
                                 $sql=$sql.' '.$key.'='.$value[1].' AND';
                                  break;
                        case 'lt':
                                 $sql=$sql.' '.$key.'<'.$value[1].' AND';
                                 break;
                        case 'elt':
                                 $sql=$sql.' '.$key.'<='.$value[1].' AND';
                                 break;
                        case 'gt':
                                $sql=$sql.' '.$key.'>'.$value[1].' AND';
                                  break;
                        case 'egt':
                                $sql=$sql.' '.$key.'>='.$value[1].' AND';
                                  break;
                    }
                }else{
                    if(is_string($value)){
                        $value='"'.$value.'"';
                    }
                    $sql=$sql.' '.$key.'='.$value.' AND';
                }
            }
            //把最后一个AND删除
            $sql=rtrim($sql,'AND');
        }


        $this->sql=$sql;
        $this->unsetWOL();
        return $this->db->excute($sql);
    }

    public function delete(){
        $sql='DELETE FROM '.$this->table;

        if($this->where){
            $sql = $sql.' WHERE ';
            foreach($this->where as $key => $value){
                if(is_array($value[0])){
                    $key=$value[0];
                    if(is_string($value[1])){
                        $value[1]='"'.$value[1].'"';
                    }
                    switch ($key){
                        case 'eq':
                            $sql=$sql.' '.$key.'='.$value[1].' AND';
                            break;
                        case 'lt':
                            $sql=$sql.' '.$key.'<'.$value[1].' AND';
                            break;
                        case 'elt':
                            $sql=$sql.' '.$key.'<='.$value[1].' AND';
                            break;
                        case 'gt':
                            $sql=$sql.' '.$key.'>'.$value[1].' AND';
                            break;
                        case 'egt':
                            $sql=$sql.' '.$key.'>='.$value[1].' AND';
                            break;
                    }
                }else{
                    if(is_string($value)){
                        $value='"'.$value.'"';
                    }
                    $sql=$sql.' '.$key.'='.$value.' AND';
                }
            }
            //把最后一个AND删除
            $sql=rtrim($sql,'AND');
        }

        $this->sql=$sql;
        $this->unsetWOL();
        return $this->db->excute($sql);
    }

    public function add($data){
        $sql='INSERT INTO '.$this->table;

        $fields='';
        $values='';
        foreach($data as $key => $value){
            $fields=$fields.$key.',';
            if(is_string($value)){
                $value='"'.$value.'"';
            }
            $values=$values.$value.',';
        }

        $fields=rtrim($fields,',');
        $values=rtrim($values,',');

        $fields='('.$fields.')';
        $values='('.$values.')';

        $sql=$sql.$fields.' values'.$values;

        $this->sql=$sql;
        $this->unsetWOL();
        return $this->db->excute($sql);
    }


    public function getLastSql(){
        return $this->sql;
    }


    private function unsetWOL(){
       $this->where=array();
      $this->orderBy=array();
      $this->limit=array();
    }

}
?>